//
//  AddCustomerViewController.m
//  KuWoCan
//
//  Created by LiBenCan on 17/9/26.
//  Copyright © 2017年 hwd. All rights reserved.
//

#import "AddCustomerViewController.h"
#import "AddCustomerCell.h"
#import "NWAddressRequest.h"

@interface AddCustomerViewController () <UITableViewDelegate,UITableViewDataSource>

@property (nonatomic, strong) AddCustomerCell *addressEditCell;

@end

@implementation AddCustomerViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setUpUI];
}
- (void)setUpUI
{
    self.view.backgroundColor = [UIColor whiteColor];
    self.title = @"客户资料";
    [self createRightButton:@"提交" selector:@selector(saveAddressAction:)];
    
    //创建列表
    _tableview = [[TPKeyboardAvoidingTableView
                   alloc] initWithFrame:CGRectMake(0, 0,SCREENW,SCREENH) style:UITableViewStyleGrouped];
    _tableview.backgroundColor = KwBgColor;
    _tableview.separatorStyle = UITableViewCellSeparatorStyleNone;
    _tableview.estimatedRowHeight = 40.0;
    _tableview.rowHeight = UITableViewAutomaticDimension;
    _tableview.delegate = self;
    _tableview.dataSource = self;
    [self.view addSubview:_tableview];

}

- (BOOL)checkInfoWithInfoArr:(NSArray *)infoArr
{
    BOOL isOk = YES;
    for (NSString *str in infoArr)
    {
        if ([CommonTool checkStringIsEmpty:str] == YES)
        {
            isOk = NO;
        }
    }
    if ([self.addressEditCell.selectBtn.titleLabel.text isEqualToString:@"点击选择"]) {
        return NO;
    }
    return isOk;
}


//保存
- (void)saveAddressAction:(UIBarButtonItem *)btn
{
 
    btn.enabled = NO;
    //判空
    if (![ToolViewViewController isNotNullStr:_addressEditCell.nameTf.text]) {
        [SVProgressHUD showErrorWithStatus:@"请添加客户名称"];
        btn.enabled = YES;
        return;
    }
    if (![ToolViewViewController isNotNullStr:_addressEditCell.teleTf.text]) {
        [SVProgressHUD showErrorWithStatus:@"请添联系人"];
         btn.enabled = YES;
        return;
    }
    if (![ToolViewViewController isNotNullStr:_addressEditCell.lianXiTf.text]) {
        [SVProgressHUD showErrorWithStatus:@"请添加联系电话"];
         btn.enabled = YES;
        return;
    }
    NSString *regionStr = @"";
    if (![_addressEditCell.selectBtn.titleLabel.text isEqualToString:@"点击选择"]) {
        regionStr =_addressEditCell.selectBtn.titleLabel.text ;
    }
    if (_isEdit) {//编辑修改状态
        dispatch_async(dispatch_get_global_queue(0, 0), ^{
            NSMutableDictionary *paraDic =@{
             @"id":[ToolViewViewController GetDataStr:_dataDic[@"id"]],
             @"user.id":[Singleton shareInstance].userId,
             @"name":_addressEditCell.nameTf.text,
             @"clientAddress":[ToolViewViewController GetDataStr:_addressEditCell.addressTf.text],
             @"clientContacts":_addressEditCell.teleTf.text,
             @"clientPhone":_addressEditCell.lianXiTf.text,
             @"clientTel":_addressEditCell.banPhoneTf.text,
             @"region":regionStr}.mutableCopy;
        [HttpRequest PostrequestUrl:[NSString stringWithFormat:@"%@/jeesite/interface/appClientInfo/editClientInfo",baseUrlMe] Dic:paraDic
                             completion:^(NSDictionary *responseDic) {
                                  btn.enabled = YES;
                                 NSLog(@"======%@",responseDic[@"msg"]);
                                 if ([responseDic[@"success"] boolValue]) {
                                     [paraDic setObject:[ToolViewViewController GetDataStr:_dataDic[@"createDate"]] forKey:@"createDate"];
                                     [paraDic setObject:[NSString stringWithFormat:@"%@%@",_addressEditCell.selectBtn.titleLabel.text,_addressEditCell.addressTf.text] forKey:@"allAddress"];
                                     if (_backBlock) {//字典回调
                                         self.backBlock(NO,paraDic);
                                     }
                                     [ToolViewViewController addPopAlterViewController:@"提交成功" Controller:self];
                                 }else
                                 {
                                     [SVProgressHUD showErrorWithStatus:@"提交失败"];
                                 }
                             }];
        });

    }else
    {
        dispatch_async(dispatch_get_global_queue(0, 0), ^{
            NSMutableDictionary *paraDic =@{
                                        @"user.id":[Singleton shareInstance].userId,
                                            @"name":_addressEditCell.nameTf.text,
                                            @"clientAddress":[ToolViewViewController GetDataStr:_addressEditCell.addressTf.text],
                                            @"clientContacts":_addressEditCell.teleTf.text,
                                            @"clientPhone":_addressEditCell.lianXiTf.text,
                                            @"clientTel":_addressEditCell.banPhoneTf.text,
                                            @"region":regionStr}.mutableCopy;
            [HttpRequest PostrequestUrl:[NSString stringWithFormat:@"%@/jeesite/interface/appClientInfo/addClientInfo",baseUrlMe] Dic:paraDic
                             completion:^(NSDictionary *responseDic) {
                                  btn.enabled = YES;
                                 NSLog(@"======%@",responseDic[@"msg"]);
                                 if ([responseDic[@"success"] boolValue]) {
                                     [paraDic setObject:[ToolViewViewController GetDataStr:_dataDic[@"createDate"]] forKey:@"createDate"];
                                     if (_backBlock) {//字典回调
                                         self.backBlock(YES,paraDic);
                                     }
                                     [ToolViewViewController addPopAlterViewController:@"提交成功" Controller:self];
                                 }else
                                 {
                                     [SVProgressHUD showErrorWithStatus:@"提交失败"];
                                 }
                             }];
        });

    }
    
   }
    


#pragma mark tableViewDelegate,DataSource

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 1;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 0.00001;
}
-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 0.00001;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *addressEditCell_ID = @"AddCustomerCell";

    _addressEditCell = [tableView dequeueReusableCellWithIdentifier:addressEditCell_ID];
    if (!_addressEditCell)
    {
        _addressEditCell = [[AddCustomerCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:addressEditCell_ID];
    }
    if (_isEdit) {
        [_addressEditCell setDataDic:_dataDic];
    }
    
    _addressEditCell.selectionStyle = UITableViewCellSelectionStyleNone;
    __weak typeof(_addressEditCell) weakCell = _addressEditCell;
    _addressEditCell.block = ^(NSString *areaStr){
        [weakCell.selectBtn setTitle:areaStr forState:UIControlStateNormal];
    };
    return _addressEditCell;

}

@end
